<?php

class Application_Model_DbTable_Reseaux extends Zend_Db_Table_Abstract
{

	protected $_name = 'reseaux';
	
public function getReseau($id)
	{
		$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		if (!$row) {
			throw new Exception("Impossible de trouver le reseau $id");
		}
		return $row->toArray();
	}

	public function addReseau($nom,$raccourci)
	{
		$data = array(
			
        	'nom'=> $nom,
		 	'raccourci' => $raccourci,
			

		);
		$this->insert($data);
	}

	public function updateReseau($id,$nom,$raccourci)
	{
		$data = array(
            'id'=> $id,
			'nom'=>$nom,
			'raccourci' => $raccourci,
        	
		);
		$this->update($data, 'id = '. (int)$id);
	}
	
public  function listReseau(){
		$select = $this->select()->setIntegrityCheck(false)
		->from(array ('r' => 'reseaux'))
		->joinInner(array ('m' => 'materiels'),'m.idReseau = r.id', array('nbMateriels'=>'COUNT(*)','nomMateriel' => 'nom'))
		->group('r.id')
		->order('m.dateAchat DESC');
			
		//echo $select;
			
		$r= $this->fetchAll($select);


		return $r;
	}
	
	public function getStatsIncidentsByReseau(){
		$select = $this->select()->setIntegrityCheck(false)
		->from(array ('i' => 'incidents'),array('nbIncidents'=>'COUNT(*)'))
		->joinInner(array ('m' => 'materiels'),'i.idMateriel = m.id', array())
		->joinInner(array ('r' => 'reseaux'),'m.idReseau = r.id', array('nomReseau'=>'r.nom'))
		->group('r.id');
		
		
		 
		$r= $this->fetchAll($select);


		return $r;
	}
	
public function getCountIncidentsByMonthAndReseau(){
		$select = $this->select()->setIntegrityCheck(false)
		->from(array ('i' => 'incidents'),array('nbIncidents'=>'COUNT(*)'))
		->joinInner(array ('m' => 'materiels'),'i.idMateriel = m.id', array())
		->joinInner(array ('r' => 'reseaux'),'m.idReseau = r.id', array('nomReseau'=>'r.nom'))
		->group('r.id')
		->having('month("datePanne")');
		
		
		 
		$r= $this->fetchAll($select);


		return $r;
	}


}

